package com.jsy.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;



@Configuration
public class RabbitConfig {

    @Autowired
    private Environment env;

    @Bean("delayQueue")
    public Queue delayQueue(){
        return QueueBuilder.durable(env.getProperty("mq.pay.queue.delay"))
                .withArgument("x-dead-letter-exchange",env.getProperty("my.pay.exchange.seckilldlx"))
                .withArgument("x-dead-letter-routing-key",env.getProperty("my.pay.routing.seckilldelaykey"))
                .build();
    }
    //死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(env.getProperty("mq.pay.queue.seckilldlx")).build();
    }

    //定义交换机
    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange(env.getProperty("my.pay.exchange.seckilldlx")).build();
    }

    //绑定队列与交换机
    @Bean
    public Binding Binding(@Qualifier("dlxExchange") Exchange exchange, @Qualifier("dlxQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(env.getProperty("my.pay.routing.seckilldelaykey")).noargs();
    }
}
